package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.WebviewActivity;
import com.hemaapp.yjnh.bean.Guide;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 类描述：
 * 创建人：GuiyouZhang
 * 创建时间：2016/9/1 9:58
 * 修改备注：
 */
public class GuideAdapter extends BaseAdapter {
    Context mContext;
    ArrayList<Guide> mDatas;

    public GuideAdapter(Context context, ArrayList<Guide> datas) {
        super(context);
        this.mContext = context;
        this.mDatas = datas;
    }

    @Override
    public int getCount() {
        return mDatas.size() == 0 ? 1 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_guide, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.iv_icon)
        ImageView ivIcon;
        @Bind(R.id.tv_content)
        TextView tvContent;
        int position = 0;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent it = new Intent(mContext, WebviewActivity.class);
                    it.putExtra("parm", mDatas.get(position).getId());
                    it.putExtra("type", 15);
                    mContext.startActivity(it);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            Guide guide = mDatas.get(position);
            ImageUtils.loadSmalImage(mContext, guide.getImgurl(), ivIcon);
            tvContent.setText(guide.getName());
        }
    }
}
